package com.spt.security.dao;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spt.fjj.service.models.common.AnnotationCommonService;
@Service
public class ShiroPermissionImpl extends AnnotationCommonService implements IShiroPermission {
    private static Logger log = LogManager.getLogger("com.spt.ws.man");

    @Override
    public List<Map<String, Object>> getPermissions(Map<String, String> order) {
        String sql = "select to_char(id) id ,permission ,describe ,createdt,creator from T_SHIRO_PERMISSION  ";
        if(order != null) {
            String col = order.get("sort");
            if(col != null) {
                sql += " order by  " + col;
                String ord = order.get("order");
                if(ord != null) {
                    sql += "  " + ord;
                }
            }
        }
        try {
            List<Map<String, Object>> li = this._queryMultex(sql);
            return li;
        }catch (Exception e) {
            log.error("取权限信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("取权限信息出错，错误信息：" + e.getMessage());
        }
        
        /*for(Map<String, Object> map : li) {
            String id = (String) map.get("ID");
            map.remove("ID");
            map.put("id", id);
        }*/
        
        //return null;
    }

    @Transactional
    @Override
    public Map<String, Object> addPermission(Map<String, String> param) throws Exception {
        String getid = "select SEQ_SECURITY.nextval from dual";
        
        String insSql = "insert into T_SHIRO_PERMISSION (id,permission,describe,createdt,creator) values(?,?,?,sysdate,?)";
        String sql = "select to_char(id) id ,permission ,describe ,createdt,creator from T_SHIRO_PERMISSION where id = ? ";
        try{
            String id = this._querySingle(getid);
            this._update(insSql,id, param.get("PERMISSION"),param.get("DESCRIBE"),param.get("creator"));
            return this._queryMultex(sql,id).get(0);
        }catch (Exception e) {
            log.error("增加权限信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("增加权限信息出错，错误信息：" + e.getMessage());
        }
        
        
    }

    @Transactional
    @Override
    public void delPermission(String permissionId) throws Exception {
        String sql1 = "delete from T_SHIRO_PERMISSION where id = ? ";
        String sql2 = "delete from T_SHIRO_ROLE_PERMISSION where permission_id = ? ";
        try{
            this._update(sql1,permissionId);
            this._update(sql2,permissionId);
        }catch (Exception e) {
            
            log.error("删除权限信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("删除权限信息出错，错误信息：" + e.getMessage());
        }
    }

    
    @Transactional
    @Override
    public Map<String, Object> modifyPermission(Map<String, String> param) {
        String update = "update T_SHIRO_PERMISSION set permission = ?,describe = ? where id = ?";
        String sql = "select to_char(id) id ,permission ,describe ,createdt,creator from T_SHIRO_PERMISSION where id = ? ";
        try {
            this._update(update, param.get("PERMISSION"),param.get("DESCRIBE"), param.get("ID"));
            return this._queryMultex(sql, param.get("ID")).get(0);
        }catch (Exception e) {
            log.error("修改权限信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("修改权限信息出错，错误信息：" + e.getMessage());
        }
    }

    @Override
    public List<Map<String, Object>> getPermissionsSimple() {
        String sql = "select to_char(id) id ,permission  from T_SHIRO_PERMISSION ";
        try {
            return this._queryMultex(sql);
        }catch (Exception e) {
            log.error("取权限信息出错，错误信息：" + e.getMessage());
            throw new RuntimeException("取权限信息出错，错误信息：" + e.getMessage());
        }
    }

}
